package com.gooagoo.supercrm.ui.activity.login;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v4.widget.CircularProgressDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.gooagoo.supercrm.MainActivity;
import com.gooagoo.supercrm.R;
import com.gooagoo.supercrm.base.BaseActivity;
import com.gooagoo.supercrm.ui.activity.home.HomeActivity;
import com.gooagoo.supercrm.utils.StrUtils;
import com.gooagoo.supercrm.utils.ToastUtil;
import com.gooagoo.supercrm.widget.CircleBackView;
import com.gooagoo.supercrm.widget.CustomDialog;
import com.gooagoo.supercrm.widget.PageStateLayout;

import org.apache.log4j.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.zhanghai.android.materialprogressbar.IndeterminateCircularProgressDrawable;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

public class LoginActivity extends BaseActivity implements LoginContract.View, View.OnClickListener {
    private static Logger logger =Logger.getLogger(MainActivity.class);
    private LoginPresenterImpl loginPresenter;

    @BindView(R.id.root_fl_login)
    FrameLayout loginFram;

    @BindView(R.id.et_tip_acount)
    EditText et_account;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.pwd_divider)
    View pwd_divider;
    private CustomDialog customDialog;
    private View loading;

    @OnClick(R.id.bt_login)
    void login(){
        loginPresenter.login(et_account.getText().toString().trim(),et_pwd.getText().toString().trim());
        }

    @Override
    protected int setContentView() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        ToastUtil.showToast(mContext,"升级成功了");
        loginPresenter = new LoginPresenterImpl();
        loginPresenter.attachView(this);
        logger.info("init come");
        immersionBar.statusBarColor(android.R.color.white).init();
        et_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!TextUtils.isEmpty(s)){
                        pwd_divider.setVisibility(View.VISIBLE);
                    }else {
                        pwd_divider.setVisibility(View.GONE);
                    }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }

    @Override
    public void showLoading() {
        loading = mInflater.inflate(R.layout.loading, null);
        immersionBar.statusBarColor(R.color.loading).init();
        FrameLayout.LayoutParams  params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
        params.gravity= Gravity.CENTER;
        loginFram.addView(loading,params);
    }

    @Override
    public void hideLoading() {
        loginFram.removeView(loading);
        immersionBar.statusBarColor(android.R.color.white).init();
    }


    @Override
    public void showErr() {
        customDialog = new CustomDialog(this, R.layout.login_err) {
            @Override
            public void initView() {
//                customDialog.findViewById(R.id.ll_login_err_qr).setOnClickListener(LoginActivity.this);
                Button btn_dlg_err_qr = customDialog.findViewById(R.id.btn_dlg_err_qr);
                btn_dlg_err_qr.setOnClickListener(LoginActivity.this);
                logger.info("initView   ");
            }
        };
        customDialog.show();
    }

    @Override
    public Context getContext() {
        return mContext;
    }


    @Override
    public void toHome() {
        startActivity(HomeActivity.class,null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_dlg_err_qr:
                customDialog.dismiss();
                break;
        }
    }
}
